package com.csxy.six.controller;

import com.csxy.six.R2;
import com.csxy.six.ResultCode;
import com.csxy.six.entity.User;
import com.csxy.six.service.UserService;

import com.csxy.six.util.OSSUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Tag(name = "用户管理", description = "用户管理相关接口")
@SecurityRequirement(name = "JWT")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private OSSUtils ossUtil;

    @GetMapping
    @Operation(summary = "获取用户列表", description = "获取所有用户列表，支持条件查询")
    public R2<List<User>> listUsers(
            @RequestParam(required = false) @Parameter(description = "用户名") String username,
            @RequestParam(required = false) @Parameter(description = "昵称") String nickname,
            @RequestParam(required = false) @Parameter(description = "状态") Integer status) {
        return userService.listUsers(username, nickname, status);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取用户详情", description = "根据ID获取用户详细信息")
    public R2<User> getUserById(@PathVariable @Parameter(description = "用户ID") Long id) {
        return userService.getUserById(id);
    }

    @PostMapping
    @Operation(summary = "创建用户", description = "创建新用户")
    public R2<User> createUser(@RequestBody @Parameter(description = "用户信息") User user) {
        return userService.createUser(user);
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新用户", description = "更新用户信息")
    public R2<User> updateUser(
            @PathVariable @Parameter(description = "用户ID") Long id,
            @RequestBody @Parameter(description = "用户信息") User user) {
        return userService.updateUser(id, user);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除用户", description = "删除指定用户")
    public R2<Void> deleteUser(@PathVariable @Parameter(description = "用户ID") Long id) {
        return userService.deleteUser(id);
    }

    @PutMapping("/{id}/status")
    @Operation(summary = "更新用户状态", description = "更新用户状态(0:禁用,1:正常,2:锁定)")
    public R2<Void> updateUserStatus(
            @PathVariable @Parameter(description = "用户ID") Long id,
            @RequestParam @Parameter(description = "状态值") Integer status) {
        return userService.updateUserStatus(id, status);
    }

    @PostMapping("/avatar/{userId}")
    @Operation(summary = "上传用户头像", description = "上传并更新用户头像图片")
    public R2<Map<String, String>> uploadAvatar(
            @PathVariable @Parameter(description = "用户ID") Long userId,
            @RequestParam("file") @Parameter(description = "头像图片文件") MultipartFile file) {
        try {
            // 使用OSS上传文件
            String url = ossUtil.upload(file);
            // 更新用户头像URL
            userService.updateAvatar(userId, url);
            return R2.ok(Collections.singletonMap("url", url));
        } catch (Exception e) {
            // 创建一个成功响应但设置为错误状态
            R2<Map<String, String>> response = R2.ok(Collections.emptyMap());
            response.setSuccess(false);
            response.setCode(ResultCode.ERROR);
            response.setMessage("头像上传失败: " + e.getMessage());
            return response;
        }
    }

    @PostMapping("/change-password")
    @Operation(summary = "修改密码", description = "修改当前用户的登录密码")
    public R2<Void> changePassword(
            @RequestParam @Parameter(description = "旧密码") String oldPassword,
            @RequestParam @Parameter(description = "新密码") String newPassword) {
        return userService.changePassword(oldPassword, newPassword);
    }
}